////////////////////////////////
//typesys.lua
//This file defines the type adjustment functions. It is used mostly to save to mysql, and process inputs.
//DEFINES
////////////////////////////////
function bbb.registertype(setup)
	if(setup==nil) then //Loser.
		bbb.error("TYPE REGISTER","Function run without input.")
		return false
	end
	if(type(setup.name)!="string") then //No name? Bah.
		bbb.error("TYPE REGISTER","Invalid name.")
		return false
	end
	if(type(setup.is)!="function") then //Bad type checking function
		bbb.error("TYPE REGISTER","Type \""..setup.name.."\" has invalid is function.")
		return false
	end
	if(type(setup.tostring)!="function") then //Invalid convert to mysql function
		bbb.error("TYPE REGISTER","Type \""..setup.name.."\" has invalid tostring function.")
		return false
	end
	if(type(setup.fromstring)!="function") then //Invalid convert from mysql function
		bbb.error("TYPE REGISTER","Type \""..setup.name.."\" has invalid fromstring function.")
		return false
	end
	
	//It must check out...
	setup.index=math.pow(2,bbb.types._number or 0) //Set the index. This is in raising powers of two, so it can be decompressed.
	bbb.types[setup.index]=setup //Store the setup table
	bbb.t[setup.name]=setup.index //Store the index, for type checking with multiple possabilities
	bbb.types._number=(bbb.types._number or 0)+1 //Increment the number
	return true
end

function bbb.istype(item,types,justone)
	if(type(types)!="number") then
		bbb.error("ISTYPE","Called without correct arguments.")
		return
	end
	local amt
	for k,v in ipairs(bbb.uncompress(types) or {}) do //For all types that it could be...
		if(v==true) then
			amt=math.pow(2,k-1) //Convert to id number
			if(bbb.types[amt].is(item)==false) then //Check against the stored function
				if(justone!=true) then //A single failed item doesn't boot the entire thing
					return false,amt //Return the failed item, if ever needed
				end
			else
				if(justone==true) then //It passed one, and it is set to justone, so it passes
					return true,amt
				end
			end
		end
	end
	
	return true
end

function bbb.typetostring(intype)
	local output={}
	for k,v in ipairs(bbb.uncompress(intype) or {}) do //For all types that it is...
		if(v==true) then
			amt=math.pow(2,k-1) //Convert to id number
			if(bbb.types[amt]!=nil) then
				table.insert(output,bbb.types[amt].name)
			end
		end
	end
	return table.concat(output," ")
end

function bbb.tostring(input,intype)
	if(type(input)!="string" or type(intype)!="string") then
		bbb.error("TOSTRING","Invalid inputs given.")
		return ""
	end
	
	if(bbb.t[intype]==nil) then
		bbb.error("TOSTRING","Type does not exist: "..intype)
		return ""
	end
	
	return bbb.types[bbb.t[intype]].tostring(input)
end

function bbb.fromstring(input,intype)
	if(type(input)!="string" or type(intype)!="string") then
		bbb.error("FROMSTRING","Invalid inputs given.")
		return ""
	end
	if(bbb.t[intype]==nil) then
		bbb.error("FROMSTRING","Type does not exist: "..intype)
		return ""
	end
	
	return bbb.types[bbb.t[intype]].fromstring(input)
end
function bbb.is(input,intype)
	if(type(intype)!="string") then
		bbb.error("IS","Invalid inputs given.")
		return false
	end
	if(bbb.t[intype]==nil) then
		bbb.error("IS","Type does not exist: "..intype)
		return false
	end
	
	return bbb.types[bbb.t[intype]].is(input)
end